/* 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef POSTITEMVIEW_H
#define POSTITEMVIEW_H

#include <QListView>
#include <QObject>
#include <QTextDocument>
#include <QMouseEvent>
#include <QResizeEvent>

#include "components/threadptr.h"
#include "components/threadid.h"
#include "components/postid.h"
#include "poststyle.h"

namespace Wishmaster
{
	class Board;
	class Chan;
}

using namespace Wishmaster; // FIXME UDOLIL!!!!!1111

class PostWidget;
class PostDocument;
class PostItemView : public QListView
{
	Q_OBJECT
public:
	PostItemView(QWidget* parent = 0);
	virtual ~PostItemView();

	void setThread(const Wishmaster::ThreadPtr& thr);
	Wishmaster::Chan* chan();
	Wishmaster::Board* board();
	Wishmaster::ThreadPtr thread();
	void updateContent();

	PostWidget* getWidget(const QModelIndex& index);
	void changed(int i);

	QString linkAt(const QPoint& p);
	QString selectedText(const QPoint& p);

	QImage imageDelete();
	QImage imageHide();
	QImage imageShow();
	QImage imageInfo();
	QImage imageUnhide();

	void resetSelections();
	void resetTexts();
	void resetFonts();
	void clearWidgets();

	void setDefaultPostFont(const QFont& font);
	QFont defaultPostFont();

	void setDefaultHeaderFont(const QFont& font);
	QFont defaultHeaderFont();

	void setDefaultNoteFont(const QFont& font);
	QFont defaultNoteFont();

	void setImageHideMode(bool s);
	bool isImageHideMode();

	void setAdjustSize(bool s);
	bool isAdjustSize();

	int maxItemWidth();

	float fontSizeMultiplier();

	void setPostStyle(const PostStylePtr& style);
	PostStylePtr postStyle() const;

signals:
	void reply(const QString& initial_text);
	void downloadAttachment(const PostId& postid, int index);
	void linkClicked(const QString& link);

public slots:
	void postDelete(const PostId& postid);
	void postHide(const PostId& postid);
	void postReply(const PostId& postid);
	void postLinkClicked(const PostId& postid);
	void postDownloadAttachment(const PostId& postid, int index);
	void slot_linkClicked(const QString& link);

	void zoom();
	void unzoom();

protected:
	virtual void resizeEvent(QResizeEvent* event);
	virtual void mousePressEvent(QMouseEvent* event);
	virtual void mouseReleaseEvent(QMouseEvent* event);
	virtual void mouseMoveEvent(QMouseEvent* event);
	virtual void wheelEvent(QWheelEvent* event);
	virtual void keyReleaseEvent(QKeyEvent* event);
	virtual void updateGeometries();

private:
	Wishmaster::Board* m_board;
	Wishmaster::ThreadPtr m_thread;
	ThreadId m_threadId;
	bool m_imageHideMode;

	int m_prevWidget;
	int m_fontSizePercents;

	QImage m_delete;
	QImage m_hide;
	QImage m_show;
	QImage m_info;
	QImage m_unhide;

	QList<PostWidget*> m_widgets;
	bool m_inMMEvent; // FIXME hack
	bool m_adjustSize;

	PostStylePtr m_postStyle;
};

#endif

